//
//  UPProductListCell.m
//  Uang Pasti
//
//  Created by 李永彬 on 2025/8/21.
//

#import "UPProductListCell.h"

@implementation UPProductListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
        [self setupConstraints];
    }
    return self;
}

- (void)setupUI {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = [UIColor clearColor];
    
    // 设置白色背景视图，作为内容容器
    self.contentView.backgroundColor = [UIColor clearColor];
    self.contentView.layer.cornerRadius = 14;
    self.contentView.layer.masksToBounds = YES;
}

- (void)setupConstraints {
    // 设置内容视图的左右边距为16
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self);
        make.left.equalTo(self).offset(16);
        make.right.equalTo(self).offset(-16);
    }];
    
    [self.containerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
    
    [self.logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(22);
        make.left.equalTo(self.contentView).offset(16);
        make.width.height.equalTo(@38);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.logoImageView);
        make.left.equalTo(self.logoImageView.mas_right).offset(8);
        make.right.equalTo(self.contentView).offset(-16);
    }];
    
    [self.rateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.logoImageView);
        make.right.equalTo(self.contentView).offset(-16);
        make.height.mas_equalTo(25);
    }];
    
    [self.applyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentView).offset(-20);
        make.right.equalTo(self.contentView).offset(-16);
        make.width.equalTo(@122);
        make.height.equalTo(@37);
    }];

    [self.amountTipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.logoImageView.mas_bottom).offset(10);
        make.left.equalTo(self.logoImageView);
        make.right.equalTo(self.applyButton.mas_left).offset(-10);
    }];
    
    [self.amountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.applyButton);
        make.left.equalTo(self.logoImageView);
    }];
}

#pragma mark - Lazy 加载组件

- (UIImageView *)logoImageView {
    if (!_logoImageView) {
        _logoImageView = [[UIImageView alloc] init];
        _logoImageView.contentMode = UIViewContentModeScaleAspectFit;
//        _logoImageView.layer.cornerRadius = 19;
        _logoImageView.layer.masksToBounds = YES;
        [self.contentView addSubview:_logoImageView];
    }
    return _logoImageView;
}

- (UIImageView *)containerImageView {
    if (!_containerImageView) {
        _containerImageView = [[UIImageView alloc] init];
        _containerImageView.contentMode = UIViewContentModeScaleAspectFit;
        _containerImageView.image = [UIImage imageNamed:@"product_cell_bg"];
        [self.contentView addSubview:_containerImageView];
    }
    return _containerImageView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        UIFont *italicFont = [UIFont italicSystemFontOfSize:16];
        UIFontDescriptor *boldItalicDescriptor = [italicFont.fontDescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold | UIFontDescriptorTraitItalic];
        _titleLabel.font = [UIFont fontWithDescriptor:boldItalicDescriptor size:16];
        _titleLabel.textColor = [UIColor colorWithHexString:@"#000000"];
        [self.contentView addSubview:_titleLabel];
    }
    return _titleLabel;
}

- (UILabel *)amountLabel {
    if (!_amountLabel) {
        _amountLabel = [[UILabel alloc] init];
        _amountLabel.font = [UIFont boldSystemFontOfSize:25];
        _amountLabel.textColor = [UIColor colorWithHexString:@"#000000"];
        _amountLabel.textAlignment = NSTextAlignmentLeft;
        [self.contentView addSubview:_amountLabel];
    }
    return _amountLabel;
}

- (UILabel *)amountTipsLabel {
    if (!_amountTipsLabel) {
        _amountTipsLabel = [[UILabel alloc] init];
        _amountTipsLabel.textColor = [UIColor colorWithHexString:@"#000000"];
        _amountTipsLabel.textAlignment = NSTextAlignmentLeft;
        UIFont *italicFont = [UIFont italicSystemFontOfSize:13];
        UIFontDescriptor *boldItalicDescriptor = [italicFont.fontDescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold | UIFontDescriptorTraitItalic];
        _amountTipsLabel.font = [UIFont fontWithDescriptor:boldItalicDescriptor size:13];
        _amountTipsLabel.text = LocalizableForKey(@"Loan amount");
        [self.contentView addSubview:_amountTipsLabel];
    }
    return _amountTipsLabel;
}

- (UILabel *)rateLabel {
    if (!_rateLabel) {
        _rateLabel = [[UILabel alloc] init];
        _rateLabel.font = [UIFont systemFontOfSize:12];
        _rateLabel.textColor = [UIColor colorWithHexString:@"#E67C00"];
        _rateLabel.backgroundColor = [[UIColor colorWithHexString:@"#FF8B00"] colorWithAlphaComponent:0.18];
        _rateLabel.layer.masksToBounds = YES;
        _rateLabel.layer.cornerRadius = 5;
        [self.contentView addSubview:_rateLabel];
    }
    return _rateLabel;
}

- (UIButton *)applyButton {
    if (!_applyButton) {
        _applyButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _applyButton.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        _applyButton.layer.cornerRadius = 6;
        _applyButton.layer.masksToBounds = YES;
        [_applyButton addTarget:self action:@selector(applyButtonTapped) forControlEvents:UIControlEventTouchUpInside];
        [_applyButton setBackgroundImage:[UIImage imageNamed:@"button_bg_style2"] forState:UIControlStateNormal];
        [_applyButton setTitleColor:[UIColor colorWithHexString:@"#BC6101"] forState:UIControlStateNormal];
        // Apply Button
        [_applyButton setTitle:LocalizableForKey(@"Apply now") forState:UIControlStateNormal];
        
        [self.contentView addSubview:_applyButton];
    }
    return _applyButton;
}

#pragma mark - 配置方法

- (void)configureWithProduct:(LeaderibleModel *)product {
    _productItem = product;
    
    // Logo
    if (product.calidine.length > 0) {
        NSString *baseStr = product.calidine;
        NSString *commonStr = [[[UPNetworkManager sharedManager] commonParameters] dicToURLQueryStr];
        NSString *imageStr = [NSString stringWithFormat:@"%@?%@",baseStr,commonStr];
        [self.logoImageView sd_setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:[UIImage imageNamed:@"launch_bg_logo"] options:SDWebImageRetryFailed];
    }
    
    // Title
    self.titleLabel.text = product.tachyion;
    
    // Amount
    self.amountLabel.text = product.buyation;
    
    // Rate
    self.rateLabel.text = [NSString stringWithFormat:@" %@ ", product.crattion ?: product.dogsome];
}

- (void)configureButtonStyleWithProduct:(LeaderibleModel *)product {
    UIColor *buttonColor = [UIColor blueColor];
    UIColor *textColor = [UIColor whiteColor];
    
    // 根据产品状态设置按钮样式
    if (product.prunity) {
        if ([product.prunity isEqualToString:@"1"]) {
            buttonColor = [UIColor colorWithRed:0.2 green:0.6 blue:0.2 alpha:1.0];
        } else if ([product.prunity isEqualToString:@"2"]) {
            buttonColor = [UIColor grayColor];
            [self.applyButton setTitle:@"待还款" forState:UIControlStateNormal];
        } else if ([product.prunity isEqualToString:@"3"]) {
            buttonColor = [UIColor lightGrayColor];
            [self.applyButton setTitle:@"已拒绝" forState:UIControlStateNormal];
        }
    }
    
    // 自定义颜色
    if (product.sileur.length > 0) {
        if ([product.sileur isEqualToString:@"yellow"]) {
            buttonColor = [UIColor yellowColor];
            textColor = [UIColor blackColor];
        } else if ([product.sileur isEqualToString:@"red"]) {
            buttonColor = [UIColor redColor];
        } else if ([product.sileur isEqualToString:@"green"]) {
            buttonColor = [UIColor greenColor];
        }
    }
    
    self.applyButton.backgroundColor = buttonColor;
    [self.applyButton setTitleColor:textColor forState:UIControlStateNormal];
}

- (void)applyButtonTapped {
    if (self.applyButtonBlock) {
        self.applyButtonBlock(self.productItem);
    }
}

@end
